package com.sc.nft.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.MyPointsDetailsVO;
import com.sc.nft.entity.vo.UserPointsDetailsStatisticsVO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsUserPageVO;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserEcologyPoints;
import com.sc.nft.mapper.UserEcologyPointsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 生态-用户积分(sc_user_ecology_points)数据DAO
 *
 * @author Sun
 * @since 2023-10-13 17:22:12
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserEcologyPointsDao extends ServiceImpl<UserEcologyPointsMapper, UserEcologyPoints> {

    public UserEcologyPoints getOneByUserId(Long userId) {
        LambdaQueryWrapper<UserEcologyPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEcologyPoints::getUserId, userId);
        return getOne(wrapper);
    }

    public Boolean subAmount(Long userId, BigDecimal amount) {
        return baseMapper.subAmount(userId, amount);
    }

    public Boolean addAmount(Long userId, BigDecimal amount) {
        LambdaUpdateWrapper<UserEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyPoints::getUserId, userId);
        wrapper.setSql("amount = amount + " + amount);
        return update(wrapper);
    }

    public BigDecimal getSumAmount() {
        return baseMapper.getSumAmount();
    }

    public Page<AdminEcologyPointsUserPageVO> getUserPage(String userTel, Integer pageNo, Integer pageSize) {
        return baseMapper.getUserPage(new Page<>(pageNo, pageSize), userTel);
    }

    public UserPointsDetailsStatisticsVO userPointsDetailsStatistics(String userTel) {
        return baseMapper.userPointsDetailsStatistics(userTel);
    }

    public MyPointsDetailsVO myPointsDetails(Long userId) {
        return baseMapper.myPointsDetails(userId);
    }

    public Boolean updateUsableAmountUpperLimitByUserId(Long userId, BigDecimal usableAmountUpperLimit) {
        LambdaUpdateWrapper<UserEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyPoints::getUserId, userId);
        wrapper.setSql("usable_amount_upper_limit = " + usableAmountUpperLimit);
        return update(wrapper);
    }

    public Boolean addUsableAmountUpperLimitByUserId(Long userId, BigDecimal usableAmountUpperLimit) {
        LambdaUpdateWrapper<UserEcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyPoints::getUserId, userId);
        wrapper.setSql("usable_amount_upper_limit = usable_amount_upper_limit + " + usableAmountUpperLimit);
        return update(wrapper);
    }

    public Boolean addEcologyPointsAndSubLimit(Long userId, BigDecimal amount) {
        return baseMapper.addEcologyPointsAndSubLimit(userId, amount);
    }
}